	#include "../include/MainHeader.h"
#define MAXSIZE 1024
#define MINSIZE 1

int create_Vfs(char *vfs_label,int size)
{

	
	long int i,j;

	

	
	int l=0;
	
	char block=' ';
	FILE *fptr;
	
	int fileFlag;
	

	//printf("size=%d\n",size );
	if(size>MAXSIZE || size<MINSIZE)
		return 4;

	//size=size+38; // Taking care of header length

	
	//nBlocks	= ((size-38)/1134);


	nBlocks=size;

//printf("\nnblocks in createvfs=%ld\n",nBlocks);

	int headerlength=60+113*nBlocks;

	
	//size=size+headerlength;


	
	fileFlag=checkNameValidity(vfs_label);

	if(!fileFlag)
	{	
		return 3;
	}

	
	if(strlen(vfs_label)>30)	
		return 5;



	
	FILE *testfp=fopen(vfs_label,"rb");
		if(testfp!=NULL)

			{	
				return 1;
			}
	
	
	
	
	//nBlocks	= ((size-38)/1134);//(30+8+4)+x(1+17+1024)=size

	strcpy(header.fileSystemLabel,vfs_label);
	//printf("no of blocks in the file=%ld\n",nBlocks);	

	header.noOfUsedFileDescriptor=1;
	header.maxNoOfFileDescriptor=nBlocks;

//	printf("header.maxNoOfFileDescriptor=%d\n",header.maxNoOfFileDescriptor );
	//creating bit-array to keep track of free blocks
	flist list[nBlocks];
	//Making an array of file descriptors
	
	struct FileDescriptor FileDescArray[nBlocks];

	// fd initialization
	


	for(l=0;l<nBlocks;l++)
	{	
		list[l].value=0;

		strcpy(FileDescArray[l].file_name,"empty1");
		strcpy(FileDescArray[l].path,"empty1");
		FileDescArray[l].fileType=' ';
		FileDescArray[l].fileSize=0;
		FileDescArray[l].blockNum=0;



	}


	//Root File Descriptor
	strcpy(FileDescArray[0].file_name,"/");
	strcpy(FileDescArray[0].path,"/");
	FileDescArray[0].fileType='D';
	FileDescArray[0].fileSize=10000;
	FileDescArray[0].blockNum=0;
	



	for(i=0;i<nBlocks;i++)
		list[i].value='0'; // initializing the bit array to '0'
	
	

	fptr=fopen(vfs_label,"w+b");
	
	if(fptr==NULL)
	{
		//printf("Unable to create file!!");
		return 2;
	}

	else
	{

		fwrite(&header,sizeof(header),1,fptr);		
		fputc('$',fptr); //Delimiter
		fwrite(list,sizeof(char),nBlocks,fptr);
		fputc('@',fptr);
		fwrite(&nBlocks,sizeof(nBlocks),1,fptr);
		fputc('#',fptr);
		fwrite(FileDescArray,sizeof(fd),nBlocks,fptr);	
		fputc('!',fptr);
	
		for(j=0;j<nBlocks;j++)
		{
			fwrite(&block,(sizeof(block)),(1024),fptr);
		}
	
		
	
		



	}

fclose(fptr);
fdcounter=header.noOfUsedFileDescriptor;
//printf("\n-------------END OF CREATEVFS----------------\n");
return 0;

}




